package com.itranswarp.mydemo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyInvocationHandler implements InvocationHandler {
    //真实对象
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    //获取proxy实例
    public Object getProxy(){
       return Proxy.newProxyInstance(this.getClass().getClassLoader() , target.getClass().getInterfaces() , this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getName().equals("eat")){
            wardenSon();
        }
       return method.invoke(target , args);
    }

    public void wardenSon(){
        System.out.println("这是区长儿子, 吃饭得吃金坷垃");
    }
}
